#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

from collections import defaultdict
from enum import Enum

from mx_rec.util.singleton import singleton


class SwapDataType(Enum):
    CONFIG = "config"
    CONTROL = "control"
    CONTROL_OPS = "control_ops"


@singleton
class SwapArgs:
    def __init__(self):
        self.swap_config_dict = defaultdict(dict)
        self.swap_control_dict = defaultdict(dict)
        self.slot_control_dict = defaultdict(dict)

    def set_data(self, data_type: str, **kwargs):
        if "var_name" not in kwargs:
            raise ValueError("Missing Required key: var_name")
        if "var_channel" not in kwargs:
            raise ValueError("Missing Required key: var_channel")
        var_name = kwargs.pop("var_name")
        var_channel = kwargs.pop("var_channel")

        if data_type == SwapDataType.CONFIG.value:
            self.swap_config_dict[var_name][var_channel] = kwargs
        elif data_type == SwapDataType.CONTROL.value:
            self.swap_control_dict[var_name][var_channel] = kwargs
        else:
            raise ValueError(f"Error data type in swap args: {data_type}")

    def set_slot_control(self, **kwargs):
        if "var_name" not in kwargs:
            raise ValueError("Missing Required key: var_name")
        var_name = kwargs.pop("var_name")
        self.slot_control_dict[var_name] = kwargs
